package com.example.weather.utils;

import android.os.Handler;

import com.thinkpage.lib.api.TPWeatherManager;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by hanjun on 2016/7/26.
 */
public class NetWorkUtils {
    private static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void getNowWeather(Handler handler, int messageType, TPWeatherManager weatherManager,Object object){
        RequestDataThread thread = new RequestDataThread(handler,messageType,weatherManager,object);
        executorService.execute(thread);
    }

    public static void sendMessageByGet(String path,String param,Handler handler,int messageType){

        HttpGetThread thread = new HttpGetThread(path,param,handler,messageType);
        executorService.execute(thread);
    }

    /**
     * 显示响应结果到命令行和TextView
     * @param response
     */
    public static String showResponseResult(HttpResponse response)
    {
        String result = "";
        if (null == response)
        {
            return null;
        }
        try
        {
            result =  EntityUtils.toString(response.getEntity(), "utf-8");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }
}
